#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "doge.h"

int mr,mc,opt = 0;
time_t time_ctr;
extern bool typing;

// Sets up the screen, and prepares the game.
// Sets ip color pairs and prepares the game for using user input.
// Displays pregame splash.
void init_menu(){
    srand(time(NULL));
    initscr();
    getmaxyx(stdscr,mr,mc);
    start_color();
    use_default_colors();
    init_pair(1,COLOR_YELLOW,-1);
    raw();
    keypad(stdscr,TRUE);
    curs_set(0);
    noecho();
    //printdoge();
    attron(COLOR_PAIR(1));
    refresh();
    attroff(COLOR_PAIR(1));
    show_menu();
}

void show_menu(){
    clear();
    //mvprintdoge(0,0);
    //attron(COLOR_PAIR(1) | A_BOLD);
    //attroff(A_BOLD);
    if (opt == 0){ attron(A_STANDOUT); }
    mvprintw(3,4,"Start");
    if (opt == 0){ attroff(A_STANDOUT); }
    if (opt == 1){ attron(A_STANDOUT); }
    mvprintw(5,4,"Exit");
    if (opt == 1){ attroff(A_STANDOUT); }
    refresh();
    switch(getch()){
    case KEY_RESIZE:
	endwin();
	getmaxyx(stdscr,mr,mc);
	refresh();
	show_menu();
	break;
    case KEY_UP:
	if (opt > 0){
	--opt;
	}
	break;
    case KEY_DOWN:
	if (opt < 1){
	    ++opt;
	}
	break;

    case 10: // ENTER
        switch(opt){
            case 0:
                start_game();
                return;
                break;
            case 1:
                endwin();
                exit(0);
                break;
        }
        break;
    }
    show_menu();
}

// Prints the time (started from a pthread).
void * print_time(void *id)
{
    int r,c; // Row and column when this is called.

    while (1)
    {
        if(typing)
        {
            getyx(stdscr,r,c);
            time(&time_ctr);
                show_time();
            move(r,c);
            refresh();
        }
        sleep(1);
    }
    return NULL;
}
